package com.newupbank.schedule.mail.task.tmp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author: liminghui
 * @date: 2021/1/17 23:09
 * @version: 1.0
 * @description: 适合从数据库只读取配置一次
 */
// @Component
// @EnableScheduling
public class MailTask1 {
    private static final Logger logger = LoggerFactory.getLogger(MailTask1.class);
    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private ResourceLoader resourceLoader;

    /*定时执行任务方法 每天5点20执行该任务*/
    // @Scheduled(cron = "0 20 5 * * *")
    // @Scheduled(cron = "${mail.task.cron}")
    public void executeTask1() throws MessagingException {
        List<String> toMailList = Arrays.asList("519355563@qq.com","lzwxfx@163.com", "205373070@qq.com","13267121193@qq.com", "13242091560@163.com");
        // List<String> toMailList = Arrays.asList("13242091560@163.com");
        executeSendMail2(toMailList);
    }

    /*定时执行任务方法 每天13点14执行该任务*/
    // @Scheduled(cron = "0 14 13 * * *")
    public void executeTask2() throws MessagingException {
        List<String> toMailList = Arrays.asList("519355563@qq.com","lzwxfx@163.com", "205373070@qq.com", "13242091560@163.com");
        executeSendMail2(toMailList);
    }

    private void executeSendMail(List<String> toMailList) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject("愿陪小可爱一辈子的老男孩！❤");
        helper.setFrom("liminghui91@foxmail.com");
        // helper.setTo(new String[]{"519355563@qq.com"});
        for (String toMail : toMailList) {
            helper.setTo(toMail);
            // helper.setCc("371820637@qq.com");
            // helper.setBcc("1406134098@qq.com");
            helper.setSentDate(new Date());
            String message = "<p><font color='D855FF'>专属你的网站</font><br/>\n" +
                    "<a href='http://qianqian.networkers.top:9999/520v1'>520happy1</a><br/>\n" +
                    "<a href='http://qianqian.networkers.top:9999/520v2'>520happy2</a><br/></p>" +
                    "<p>hello 小可爱好久不见呀<br/>\n" +
                    "    夏至未至<br/>\n" +
                    "    我的茉莉花因你而绽放<br/>\n" +
                    "    我的心因你而热血沸腾<br/>\n" +
                    "    春风十里不如你<br/>\n" +
                    "    期待你的回应<br/>\n" +
                    "    不管结果如何<br/>\n" +
                    "    我都会一如既往的支持你的决定<br/>\n" +
                    "    只愿你过的日子永远“小可爱”<br/>\n" +
                    "    就像照片一样真实笑容美丽<br/>\n" +
                    "    工作虽苦，愿你的生活，爱情甜一点喲~~<br/></p><p>不由自觉得想起一首歌《想起》：回到相遇的地点，才知我对你不了解，以为爱得深就不怕伤悲，偏偏爱让心成雪，我独自走在寂寞的长街，回忆一幕幕重演，我告诉自己勇敢去面对，就算心碎也完美...</p><p>图1(相遇五彩桥)：</p><img src='cid:p01'/><p>图2(离别地铁站)：</p><img src='cid:p02'/><p>图3(最美小可爱)：</p><img src='cid:p03'/>";
            message += "\r\n--威风少侠--";
            helper.setText(message, true);

            Resource resource1 = resourceLoader.getResource("classpath:static/img/img1.jpg");
            Resource resource2 = resourceLoader.getResource("classpath:static/img/img2.jpg");
            Resource resource3 = resourceLoader.getResource("classpath:static/img/img3.jpg");
            helper.addInline("p01", resource1);
            helper.addInline("p02", resource2);
            helper.addInline("p03", resource3);

            // ClassPathResource classPathResource1 = new ClassPathResource("static/img/img1.jpg");
            // ClassPathResource classPathResource2 = new ClassPathResource("static/img/img2.jpg");
            // ClassPathResource classPathResource3 = new ClassPathResource("static/img/img3.jpg");
            // helper.addInline("p01", classPathResource1);
            // helper.addInline("p02", classPathResource2);
            // helper.addInline("p03", classPathResource3);

            // helper.addInline("p01", new FileSystemResource(new File("img/img1.jpg")));
            // helper.addInline("p02", new FileSystemResource(new File("img/img2.jpg")));
            // helper.addInline("p03", new FileSystemResource(new File("img/img3.jpg")));
            javaMailSender.send(mimeMessage);
        }

        logger.info("MailTask1--->executeTask is successful");
    }


    private void executeSendMail2(List<String> toMailList) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject("七夕快乐❤❤❤");
        helper.setFrom("liminghui91@foxmail.com");
        // helper.setTo(new String[]{"519355563@qq.com"});
        for (String toMail : toMailList) {
            helper.setTo(toMail);
            // helper.setCc("371820637@qq.com");
            // helper.setBcc("1406134098@qq.com");
            helper.setSentDate(new Date());
            String message =
                    "<html><body><p>图1(七夕快乐)：<img src='cid:p01'/></p>" +
                    "<p>    天上群星参北斗，<br/>" +
                    "    天下自古真情有，<br/>" +
                    "    相思之苦织女情，<br/>" +
                    "    你瞧牛郎多憨厚。<br/>" +
                    "    时传梁祝痴情留，<br/>" +
                    "    时有翁帆牵杨手，<br/>" +
                    "    念念难忘情似海，<br/>" +
                    "    葡萄架下话相思！<br/></p>" +
                            "<p>图2(仙女下凡)：<br/><img src='cid:p02'/></p>";
            message += "--威风少侠--</body></html>";
            helper.setText(message, true);

            Resource resource1 = resourceLoader.getResource("classpath:static/img/77.jpg");
            Resource resource2 = resourceLoader.getResource("classpath:static/img/img4.jpg");
            helper.addInline("p01", resource1);
            helper.addInline("p02", resource2);

            // ClassPathResource classPathResource1 = new ClassPathResource("static/img/img1.jpg");
            // ClassPathResource classPathResource2 = new ClassPathResource("static/img/img2.jpg");
            // ClassPathResource classPathResource3 = new ClassPathResource("static/img/img3.jpg");
            // helper.addInline("p01", classPathResource1);
            // helper.addInline("p02", classPathResource2);
            // helper.addInline("p03", classPathResource3);

            // helper.addInline("p01", new FileSystemResource(new File("img/img1.jpg")));
            // helper.addInline("p02", new FileSystemResource(new File("img/img2.jpg")));
            // helper.addInline("p03", new FileSystemResource(new File("img/img3.jpg")));
            javaMailSender.send(mimeMessage);
        }

        logger.info("MailTask1--->executeTask is successful");
    }

}
